import { ecs } from "../../../../core/ecs/ECS";

/**
 * 等级组件
 * 
 * 通用组件：可用于 Player、Enemy、NPC 等有等级的实体
 * 
 * @example
 * const player = ecs.getEntity(Player);
 * player.LevelComponent.addExperience(150);
 * console.log(`Level: ${player.LevelComponent.level}`);
 */
@ecs.register('LevelComponent')
export class LevelComponent extends ecs.Comp {
    /** 当前等级 */
    public level: number = 1;
    
    /** 当前经验值 */
    public experience: number = 0;
    
    /**
     * 重置组件数据
     */
    reset(): void {
        this.level = 1;
        this.experience = 0;
    }
    
    /**
     * 设置等级
     */
    setLevel(level: number): this {
        this.level = Math.max(1, level);
        return this;
    }
    
    /**
     * 添加经验值
     * @param exp 经验值
     * @returns 是否升级了
     */
    addExperience(exp: number): boolean {
        this.experience += exp;
        
        const expNeeded = this.getExpForNextLevel();
        if (this.experience >= expNeeded) {
            this.levelUp();
            this.experience -= expNeeded;
            return true;
        }
        
        return false;
    }
    
    /**
     * 升级
     */
    levelUp(): void {
        this.level++;
    }
    
    /**
     * 获取下一级所需经验值
     */
    getExpForNextLevel(): number {
        // 简单的升级公式：level * 100
        return this.level * 100;
    }
    
    /**
     * 获取当前等级经验进度百分比 (0-100)
     */
    getExpPercentage(): number {
        const expNeeded = this.getExpForNextLevel();
        return expNeeded > 0 ? (this.experience / expNeeded) * 100 : 0;
    }
}

